package account

import (
  "encoding/json"
  "fmt"
  "gitee.com/yizhisamoye/GoWeChat/entity/user"
  "gitee.com/yizhisamoye/GoWeChat/utils"
  "strconv"
)

/**
用户标签
*/

func (w WxAccount) CreateUserTag(tag user.WxUserTag) (*user.WxUserTag, error) {
  url := fmt.Sprintf(BaseUrl+"/cgi-bin/tags/create?access_token=%s", w.accessToken)
  result, err := utils.SendJson(url, map[string]user.WxUserTag{
    "tag": tag,
  })
  if err != nil {
    return nil, err
  }
  tagMap := result["tag"].(map[string]interface{})
  tag.Id = int(tagMap["id"].(float64))
  return &tag, nil
}
func (w WxAccount) DeleteUserTag(tag user.WxUserTag) error {
  url := fmt.Sprintf(BaseUrl+"/cgi-bin/tags/delete?access_token=%s", w.accessToken)
  _, err := utils.SendJson(url, map[string]user.WxUserTag{
    "tag": tag,
  })
  return err
}
func (w WxAccount) UpdateUserTag(tag user.WxUserTag) error {
  url := fmt.Sprintf(BaseUrl+"/cgi-bin/tags/update?access_token=%s", w.accessToken)
  _, err := utils.SendJson(url, map[string]user.WxUserTag{
    "tag": tag,
  })
  return err
}
func (w WxAccount) GetUserTags() ([]user.WxUserTag, error) {
  url := fmt.Sprintf(BaseUrl+"/cgi-bin/tags/get?access_token=%s", w.accessToken)
  result, err := utils.GetJson(url)
  if err != nil {
    return nil, err
  }
  tags := result["tags"].([]interface{})
  wxTags := make([]user.WxUserTag, 0)
  for _, v := range tags {
    tmp, _ := json.Marshal(v)
    wxTag := new(user.WxUserTag)
    json.Unmarshal(tmp, wxTag)
    wxTags = append(wxTags, *wxTag)
  }
  return wxTags, nil
}

//获取标签下粉丝列表
func (w WxAccount) GetUserListOfTag(tag user.WxUserTag, nextOpenId ...string) (map[string]interface{}, error) {
  url := fmt.Sprintf(BaseUrl+"/cgi-bin/user/tag/get?access_token=%s", w.accessToken)
  data := map[string]string{
    "tagid": strconv.Itoa(tag.Id),
  }
  if len(nextOpenId) > 0 {
    data["next_openid"] = nextOpenId[0]
  }
  result, err := utils.SendJson(url, data)
  return result, err
}

//批量为用户打标签
func (w WxAccount) TagUserBatch(users []user.WxUser, tag user.WxUserTag) error {
  url := fmt.Sprintf(BaseUrl+"/cgi-bin/tags/members/batchtagging?access_token=%s", w.accessToken)
  data := map[string]interface{}{
    "tagid": tag.Id,
  }
  openIds := make([]string, 0)
  for _, v := range users {
    openIds = append(openIds, v.OpenId)
  }
  data["openid_list"] = openIds
  _,err:=utils.SendJson(url, data)
  return err
}
//批量为用户取消标签
func (w WxAccount) UnTagUserBatch(users []user.WxUser, tag user.WxUserTag)error{
  url := fmt.Sprintf(BaseUrl+"/cgi-bin/tags/members/batchuntagging?access_token=%s", w.accessToken)
  data := map[string]interface{}{
    "tagid": tag.Id,
  }
  openIds := make([]string, 0)
  for _, v := range users {
    openIds = append(openIds, v.OpenId)
  }
  data["openid_list"] = openIds
  _,err:=utils.SendJson(url, data)
  return err
}
//获取用户身上的标签列表
func (w WxAccount) GetTagListOfUser(user user.WxUser) ([]int,error){
  url := fmt.Sprintf(BaseUrl+"/cgi-bin/tags/getidlist?access_token=%s", w.accessToken)
  data := map[string]string{
    "openid": user.OpenId,
  }
  result,err:=utils.SendJson(url, data)
  if err!=nil{
    return nil,err
  }
  tagListTemp:=result["tagid_list"].([]interface{})
  tagList:=make([]int,0)
  for _,v:=range tagListTemp{
    tagList=append(tagList,int(v.(float64)))
  }
 return tagList,nil
}
